/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanyq.reactive.reactor;

import com.yanyq.reactive.streams.BusinessSubscriber;
import com.yanyq.reactive.streams.SimplePublisher;
import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;
import reactor.core.CoreSubscriber;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;


public class MonoDemo {

    public static void main(String[] args) throws InterruptedException {

        SimplePublisher publisher = new SimplePublisher();
        Mono.from(publisher)
                .subscribe(new BusinessSubscriber(1));
        for (int i = 0; i < 5; i++) {
            publisher.publish(i);
        }
    }

    private static void demoMonoPublisher() {
//        SimplePublisher publisher = new SimplePublisher();
//
//        publisher.subscribe(new BusinessSubscriber(1));
//
//        for (int i = 0; i < 5; i++) {
//            publisher.publish(i);
//        }



        SimplePublisher publisher = new SimplePublisher();
        Mono.from(publisher)
                .subscribe(new BusinessSubscriber(5));


        for (int i = 0; i < 5; i++) {
            publisher.publish(i);
        }

//        Supplier<String> supplier = () -> "Hello,World";
//
//        Mono.fromSupplier(supplier)
//                .subscribe(data -> {
//                    System.out.printf("[Thread : %s] %s\n", Thread.currentThread().getName(), data);
//                });
    }

    public static void d(){
        SimplePublisher publisher = new SimplePublisher();
        //Mono.from(publisher)
        // Flux.just(1,2,3,4,5)
        Mono.just(1)
                .subscribe(new CoreSubscriber<Object>() {//这里传入CoreSubscriber对象作为订阅者
                    @Override
                    public void onSubscribe(Subscription s) {
                        System.out.println("onSubscribe, " + s.getClass());
                        s.request(5);
                    }

                    @Override
                    public void onNext(Object integer) {
                        System.out.println("onNext： " + integer);
                    }
                    @Override
                    public void onError(Throwable t) {
                    }
                    @Override
                    public void onComplete() {
                        System.out.println("onComplete");
                    }
                });
        for (int i = 0; i < 5; i++) {
            publisher.publish(i);
        }
    }

    public static void d2(){
        SimplePublisher publisher = new SimplePublisher();
        //Mono.from(publisher)
         Flux.just(1,2,3,4,5)
                .subscribe(new CoreSubscriber<Object>() {//这里传入CoreSubscriber对象作为订阅者
                    @Override
                    public void onSubscribe(Subscription s) {
                        System.out.println("onSubscribe, " + s.getClass());
                        s.request(5);
                    }

                    @Override
                    public void onNext(Object integer) {
                        System.out.println("onNext： " + integer);
                    }
                    @Override
                    public void onError(Throwable t) {
                    }
                    @Override
                    public void onComplete() {
                        System.out.println("onComplete");
                    }
                });
        for (int i = 0; i < 5; i++) {
            publisher.publish(i);
        }
    }

}

